using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace SPHelpCollectionBuilder
{
    public partial class NewProjectForm : Form
    {
        private string _manifestId = string.Empty;

        public string ManifestId
        {
            get { return _manifestId; }
            set { _manifestId = value; }
        }

        private string _folderPath = string.Empty;

        public string FolderPath
        {
            get { return _folderPath; }
            set { _folderPath = value; }
        }
        private string _projectName = string.Empty;

        public string ProjectName
        {
            get { return _projectName; }
            set { _projectName = value; }
        }

        public NewProjectForm()
        {
            InitializeComponent();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            DialogResult dResult = folderBrowserDialog.ShowDialog(this);
            if (dResult == DialogResult.OK)
            {
                textBoxFolder.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private bool ValidateInput(out string errorMessage)
        {
            errorMessage = string.Empty;
            if (string.IsNullOrEmpty(textBoxProjectName.Text))
            {
                errorMessage = "Enter a project name";
                return false;
            }

            if (string.IsNullOrEmpty(textBoxFolder.Text))
            {
                errorMessage = "Specify a folder";
                return false;
            }

            if(string.IsNullOrEmpty(textBoxManifestId.Text))
            {
                errorMessage = "Specify a manifest Id";
                return false;
            }

            return true;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            textBoxManifestId.Text = TidyStringRemoveNonAlphanumericExceptDot(textBoxManifestId.Text);
            string validationError = string.Empty;
            if (ValidateInput(out validationError))
            {
                this.FolderPath = textBoxFolder.Text;
                this.ProjectName = textBoxProjectName.Text;
                this.DialogResult = DialogResult.OK;
                _manifestId = textBoxManifestId.Text;
                if (!_manifestId.ToLower().EndsWith(".manifest"))
                    _manifestId = string.Concat(_manifestId, ".manifest");

                _manifestId = _manifestId.ToUpper();
            }
            else
            {
                MessageBox.Show(validationError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private string TidyStringRemoveNonAlphanumericExceptDot(string input)
        {
            /*
             * .check see whether input id has non-alphanumerica characters, except ., . is ok
             * .if so, tidy it up and warn user
             */

            MatchCollection matches = Regex.Matches(input, "\\W");
            if (matches != null)
            {
                foreach (Match m in matches)
                {
                    if (m.Value != ".")
                    {
                        input = input.Replace(m.Value, string.Empty);
                    }
                }
            }

            return input;
        }

        private void textBoxManifestId_Leave(object sender, EventArgs e)
        {
            textBoxManifestId.Text = TidyStringRemoveNonAlphanumericExceptDot(textBoxManifestId.Text);
        }

        private void NewProjectForm_Load(object sender, EventArgs e)
        {

        }
    }
}